<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;

class StudentController extends BaseController {

	public function index() {
		$this->assign('students', D('Student')->relation('class')->select());
		$this->display();
	}

	public function add() {
		$this->_add();
		$this->display();
	}

	public function addPost() {
		if(!IS_POST) {
			$this->redirect('Student/index');
		}
		$student = D('Student');
		if(!$student->create(I('post.'), 1)) {
			$this->error($student->getError());
		}
		$data = $student->data();
		$map['major_id'] = (int)I('post.major_id');
		$map['grade'] = (int)I('post.grade');
		$map['class'] = (int)I('post.class');
		$result = M('Class')->where($map)->find();
		if(!$result) {
			$this->error('专业、年级、班号有误！', U('Student/add'));
		}
		$data['class_id'] = (int)$result['id'];
		if(!D('Student')->data($data)->add()) {
			$this->error('数据写入失败！', U('Student/add'));
		}
		$this->success('添加成功！', U('Student/index'), 0);
	}

	public function addBatch() {
		$this->_add();
		$this->display();
	}

	public function addBatchPost() {
		if(!IS_POST) {
			$this->redirect('Student/index');
		}
		// 查找班级
		$map['major_id'] = (int)I('post.major_id');
		$map['grade'] = (int)I('post.grade');
		$map['class'] = (int)I('post.class');
		$result = M('Class')->where($map)->find();
		if(!$result) {
			$this->error('专业、年级、班号有误！', U('Student/add'));
		}
		$data['class_id'] = (int)$result['id'];

		// 上传文件
		$upload = new \Think\Upload();
		$upload->rootPath = './Application/Runtime/Temp/';
		$upload->savePath = '';
		$upload->autoSub = false;
		$upload->exts = array('xls', 'xlsx');
		$result = $upload->upload();
		if(!$result) {
			$this->error($upload->getError());
		}

		// 读取文件
		$filename = $upload->rootPath . $result['file']['savepath'] . $result['file']['savename'];
		$filetype = pathinfo($filename, PATHINFO_EXTENSION);
		import('Org.Util.PHPExcel');
		import('Org.Util.PHPExcel.IOFactory');
		$objPHPExcel = \PHPExcel_IOFactory::load($filename);
		$sheet = $objPHPExcel->getSheet(0);
		$highestRowNumber = $sheet->getHighestRow();
		$students = array();
		for($i = 1; $i <= $highestRowNumber; $i++) {
			$cellAVal = $sheet->getCell('A' . $i)->getValue();
			$cellBVal = $sheet->getCell('B' . $i)->getValue();
			$students[] = array($cellAVal, $cellBVal);
		}
		
		// 读取文件后删除
		unlink($filename);

		// 验证数据
		$student = D('Student');
		foreach($students as $k => $row) {
			$data['id'] = $row[0];
			$data['name'] = $row[1];
			if(!$student->create($data, 1)) {
				$this->error('第' . ($k + 1) . '行数据有误：' . $student->getError(), U('Student/addBatch'));
			}
		}

		// 写入数据
		foreach($students as $k => $row) {
			$data['id'] = $row[0];
			$data['password'] = md5($row[0]);
			$data['name'] = $row[1];
			if(!$student->data($data)->add()) {
				$this->error('数据写入失败！');
			}
		}

		$this->success('导入成功！', U('Student/index'), 0);
	}

	public function deletePost() {
		if(!IS_POST) {
			$this->redirect('Student/index');
		}
		$id = (int)I('post.id');
		$student = D('Student');
		if($student->hasChildObj($id)) {
			$this->error($student->getError());
		}
		if(!M('Student')->where(array('id' => $id))->delete()) {
			$this->error('删除失败！');
		}
		$this->success('删除成功！', U('Student/index'), 0);
	}

	public function modify() {
		$id = I('id');
		$this->_detail($id);
		$this->display();
	}

	public function modifyPost() {
		if(!IS_POST) {
			$this->redirect('Student/index');
		}
		$id = (int)I('post.id');
		$student = D('Student');
		if(!$student->create()) {
			$this->error($student->getError(), U('Student/modify', array('id' => $id)));
		}
		$map['major_id'] = (int)I('major_id');
		$map['grade'] = (int)I('grade');
		$map['class'] = (int)I('class');
		$class = M('Class')->where($map)->find();
		if($map['major_id'] && !$class) {
			$this->error('专业、年级、班号填写有误！', U('Student/modify', array('id' => $id)));
		}
		$data = $student->data();
		$data['class_id'] = (int)$class['id'];
		$result = $student->data($data)->save();
		if(false === $result) {
			$this->error('数据更新失败！', U('Student/modify', array('id' => $id)));
		}
		$this->success('修改成功！', U('Student/index'), 0);
	}

	public function detail() {
		$id = I('id');
		$this->_detail($id);
		$this->display();
	}

	public function getGradesByMajorId() {
		$map['major_id'] = I('major_id');
		$ret = M('Class')->where($map)->distinct(true)->field('grade')->order('grade')->select();
		$this->ajaxReturn($ret);
	}

	public function getClassNumbers() {
		$map['major_id'] = I('major_id');
		$map['grade'] = I('grade');
		$ret = M('Class')->where($map)->distinct(true)->field('class')->order('class')->select();
		$this->ajaxReturn($ret);
	}

	private function _add() {
		$academies = D('Academy')->relation('majors')->select();
		$academyId = (int)$academies[0]['id'];
		$majors = $academies[0]['majors'];
		$majorId = (int)$majors[0]['id'];
		$map['major_id'] = $majorId;
		$grades = M('Class')->where($map)->distinct(true)->field('grade')->select();
		$grade = (int)$grades[0]['grade'];
		$map['grade'] = $grade;
		$classNumbers = M('Class')->where($map)->distinct(true)->field('class')->select();
		$classNumber = $classNumbers[0]['class'];
		$this->assign('academies', $academies);
		$this->assign('academy_id', $academyId);
		$this->assign('majors', $majors);
		$this->assign('major_id', $majorId);
		$this->assign('grades', $grades);
		$this->assign('grade', $grade);
		$this->assign('class_numbers', $classNumbers);
		$this->assign('class_number', $classNumber);
	}

	private function _detail($id) {
		$student = D('Student')->where(array('id' => $id))->relation('class')->find();
		$studentId = (int)$student['id'];
		$majorId = (int)$student['class']['major_id'];
		$major = D('Major')->where(array('id' => $majorId))->relation('academy')->find();
		$academyId = (int)$major['academy_id'];
		$majors = M('Major')->where(array('academy_id' => $academyId))->select();
		$map['major_id'] = $majorId;
		$grades = M('Class')->where($map)->distinct(true)->field('grade')->order('grade')->select();
		$grade = (int)$student['class']['grade'];
		$map['grade'] = $grade;
		$classNumbers = M('Class')->where($map)->distinct(true)->field('class')->order('class')->select();
		$classNumber = (int)$student['class']['class'];
		$this->assign('student', $student);
		$this->assign('academy_id', $academyId);
		$this->assign('academies', M('Academy')->select());
		$this->assign('major_id', $majorId);
		$this->assign('majors', $majors);
		$this->assign('grade', $grade);
		$this->assign('grades', $grades);
		$this->assign('class_number', $classNumber);
		$this->assign('class_numbers', $classNumbers);		
	}

	public function test() {
		$filename = './Application/Runtime/Temp/5743f8dbcb0d5.xlsx';
		import('Org.Util.PHPExcel');
		import('Org.Util.PHPExcel.Reader.Excel2007');
		$objPHPReader = new \PHPExcel_Reader_Excel2007();
		$objPHPExcel = $objPHPReader->load($filename);
		$sheet = $objPHPExcel->getSheet(0);
		$highestRowNumber = $sheet->getHighestRow();
		$highestColumn = $sheet->getHighestColumn();
		$students = array();
		for($i = 1; $i <= $highestRowNumber; $i++) {
			$cellAVal = $sheet->getCell('A' . $i)->getValue();
			$cellBVal = $sheet->getCell('B' . $i)->getValue();
			$students[] = array($cellAVal, $cellBVal);
		}
		dump($students);
	}

}
